package com.wunong.smart.collection.domain.state.operation;

import com.wunong.smart.collection.application.context.CollectionContext;
import com.wunong.smart.collection.application.request.BaseCollectionRequest;
import com.wunong.smart.collection.repository.CollectionRepository;
import com.wunong.smart.collection.domain.state.CollectionStateService;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * @author create by zealot.zt
 */
@Slf4j
public abstract class AbstractCollectionOperation<REQ extends BaseCollectionRequest> implements CollectionOperation<REQ> {

    @Resource
    protected CollectionRepository collectionRepository;

    @Resource
    protected CollectionStateService collectionStateService;

    @Override
    public Object operate(CollectionContext<REQ> context) {
        beforeOperate(context);
        Object result = doOperate(context);
        afterOperate(context, result);
        return result;
    }

    protected void beforeOperate(CollectionContext<REQ> context) {
        // 开始操作之前
    }

    /**
     * 执行对应的操作
     *
     * @param context
     * @return
     */
    protected abstract Object doOperate(CollectionContext<REQ> context);

    /**
     * 操作成功，记录对应的动作记录
     *
     * @param context
     * @param result
     */
    protected void afterOperate(CollectionContext<REQ> context, Object result) {
        // do nothing
    }

}
